package com.mindainfo.groundwave.web.member.web;

import com.mindainfo.groundwave.common.api.member.param.EmpQueryListParam;
import com.mindainfo.groundwave.common.api.member.param.EmpSaveParam;
import com.mindainfo.groundwave.common.api.member.service.IDeptService;
import com.mindainfo.groundwave.common.api.member.service.IEmployeeService;
import com.mindainfo.groundwave.common.api.member.vo.DeptGetMyDeptsVo;
import com.mindainfo.groundwave.common.api.member.vo.EmpQueryListVo;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.TableData;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by MD-01 on 2017/7/3.
 */
@Controller
@RequestMapping("/sys/employee")
public class EmployeeController {

    @Resource
    private IEmployeeService employeeService;
    @Resource
    private IDeptService deptService;



    @RequestMapping("/page")
    public String page(HttpServletRequest request){

        Subject subject = SecurityUtils.getSubject();
        boolean permitted = subject.isPermitted("member:changePassword");
        System.out.println("permitted:"+permitted);


        CommonResult<List<DeptGetMyDeptsVo>> deptResult = deptService.getMyDepts();
        request.setAttribute("depts",deptResult.getData());
        return "/member/employee";
    }

    @RequestMapping("/queryList")
    @ResponseBody
    public TableData<EmpQueryListVo> queryList(Integer start,Integer length,
                                               String key,String draw){

        EmpQueryListParam param = new EmpQueryListParam();
        param.setStart(start);
        param.setNum(length);
        param.setKey(key);

        CommonResult<TableData<EmpQueryListVo>> result = employeeService.queryList(param);
        result.getData().setDraw(draw);
        return result.getData();
    }

    @RequestMapping("/save")
    @ResponseBody
    public CommonResult<Void> save(Integer employeeId,String name,
                                   String post,String phoneNum,Integer deptId){

        EmpSaveParam param = new EmpSaveParam();
        param.setEmployeeId(employeeId);
        param.setName(name);
        param.setPhoneNum(phoneNum);
        param.setPost(post);
        param.setDeptId(deptId);
        return employeeService.save(param);
    }
}
